﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace f1
{
    using System.Xml.Serialization;
    using System.IO;
    using System.Runtime.Serialization;
    [Serializable]
    public class Order
    {

        private int id;
        private string customer;
        private int amount;
        private List<OrderDetail> items;
        public Order() { }
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public string Customer
        {
            get
            {
                return customer;
            }
            set
            {
                customer = value;
            }
        }
        public int Amount
        {
            get
            {
                return amount;
            }
            set
            {
                amount = value;
            }
        }
        public List<OrderDetail> Items
        {
            get { return items; }
            set { items = value; }
        }
        public Order(int id, string customer, int amount, List<OrderDetail> items = null)
        {
            this.id = id;
            this.customer = customer;
            this.amount = amount;
            if (items != null)
                this.items = items;
            else
                this.items = new List<OrderDetail>();
        }
        public override string ToString()
        {
            return "订单号：" + this.id + "\t客户：" + this.customer + "\t价格：" + this.amount;
        }
        public void ShowAllDetails()
        {
            Console.WriteLine("订单" + id + "\t共" + items.Count + "个商品：");
            foreach (OrderDetail d in items)
                Console.WriteLine(d);
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Order other = obj as Order;
            if(other==null) return false;
            return other.id == Id;
        }
        public override int GetHashCode()
        {
            return Id;
        }
    }
    [Serializable]
    public class OrderDetail
    {
        private int orderId;
        private string name;
        private int num;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public int Num
        {
            get
            {
                return num;
            }
            set
            {
                num = value;
            }
        }
        public int OrderId
        {
            get
            {
                return orderId;
            }
            set
            {
                orderId = value;
            }
        }
        public OrderDetail()
        {

        }
        public OrderDetail(int id, string name, int num)
        {
            this.orderId = id;
            this.name = name;
            this.num = num;
        }
        public override string ToString()
        {
            return "ID:" + orderId + "名称：" + this.name + "\t数量：" + this.num;
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            OrderDetail o = obj as OrderDetail;
            return o.orderId == orderId;
        }
        public override int GetHashCode()
        {
            return orderId;
        }
    }

    public class OrderService
    {
        public List<Order> orders = new List<Order>();


        public void AddOrder(int id,string customer, int amount, List<OrderDetail> list)
        {
            orders.Add(new Order(id, customer, amount, list));
        }
        public void AddOrder()
        {
            int num, name, cnt;
            string customer;
            int id, amount;
            id = orders.Count;
            Console.WriteLine("客户名为：");
            customer = Console.ReadLine();
            Console.WriteLine("总价为：");
            amount = Int32.Parse(Console.ReadLine());
            Console.WriteLine("订单商品种类数为：");
            num = Int32.Parse(Console.ReadLine());
            List<OrderDetail> list = new List<OrderDetail>();

            for (int i = 1; i <= num; i++)
            {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.OrderId = id * 10000 + i;
                Console.WriteLine($"第{i}件商品的名称为：");
                orderDetail.Name = Console.ReadLine();
                Console.WriteLine($"第{i}件商品的数量为：");
                orderDetail.Num = Int32.Parse(Console.ReadLine());
                list.Add(orderDetail);
            }
            Order order = new Order(id, customer, amount, list);
            orders.Add(order);
        }
        public bool DelOrder1(int id)
        {
            var query = from s in orders
                        where id == s.Id
                        select s;
            if (query.Count() == 0)
            {
                Console.WriteLine("订单不存在！");
                return false;
            }
            List<Order> list = query.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                orders.Remove(list[i]);
                Console.WriteLine("删除成功！");
            }
            return true;
        }

        public void DelOrder()
        {
            int id;
            Console.WriteLine("输入想要删除订单的ID:");
            id = Int32.Parse(Console.ReadLine());
            var query = from s in orders
                        where id == s.Id
                        select s;
            if (query.Count() == 0)
            {
                Console.WriteLine("订单不存在！");
                return;
            }
            List<Order> list = query.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                orders.Remove(list[i]);
                Console.WriteLine("删除成功！");
            }
        }
        public void searchAll()
        {
            if (orders.Count == 0)
            {
                Console.WriteLine("无订单");
                return;
            }
            for (int i = 0; i < orders.Count; i++)
            {
                Console.WriteLine(orders[i]);
                orders[i].ShowAllDetails();
            }
        }
        public void searchByID()
        {
            Console.WriteLine("输入订单号：");
            int id = Int32.Parse(Console.ReadLine());
            var query = from s in orders
                        where s.Id == id
                        orderby s.Amount descending
                        select s;
            List<Order> list = query.ToList();
            if (query.Count() == 0)
            {
                Console.WriteLine("无订单");
            }
            foreach (Order order in list)
            {
                Console.WriteLine(order);
                order.ShowAllDetails();
            }
        }
        public void searchByCus()
        {
            Console.WriteLine("输入客户名称：");
            string name = Console.ReadLine();
            var query = from s in orders
                        where s.Customer == name
                        orderby s.Amount descending
                        select s;
            List<Order> order = query.ToList();
            if (order.Count == 0)
            {
                Console.WriteLine("订单不存在！");
                return;
            }
            foreach (Order s in order)
            {
                Console.WriteLine(s);
                s.ShowAllDetails();
            }
        }
        public void searchByAmount()
        {
            Console.WriteLine("请输入订单金额：");
            int amount = Int32.Parse(Console.ReadLine());
            var query = from s in orders
                        where s.Amount == amount
                        select s;
            List<Order> order = query.ToList();
            if (order.Count == 0)
            {
                Console.WriteLine("订单不存在！");
                return;
            }
            foreach (Order s in order)
            {
                Console.WriteLine(s);
                s.ShowAllDetails();
            }
        }
        public void ChangeOrder()
        {
            Console.WriteLine("输入订单号：");
            int id = Int32.Parse(Console.ReadLine());
            var query = from s in orders
                        where s.Id == id
                        orderby s.Amount descending
                        select s;
            List<Order> list = query.ToList();
            if (list.Count == 0)
            {
                Console.WriteLine("订单不存在！");
                return;
            }
            foreach (Order order in list)
            {
                Console.WriteLine(order);
                order.ShowAllDetails();
            }
            Console.WriteLine("输入想要更改的项：");
            Console.WriteLine("1-订单客户 2-订单价格");
            int n = Int32.Parse(Console.ReadLine());
            switch (n)
            {
                case 1:
                    Console.WriteLine("输入更改后客户名");
                    string name = Console.ReadLine();
                    orders[id].Customer = name;
                    break;
                case 2:
                    Console.WriteLine("输入更改后客户名");
                    int prize = Int32.Parse(Console.ReadLine());
                    orders[id].Amount = prize;
                    break;
                default: Console.WriteLine("错误"); break;
            }
        }
        public void Export()
        {
            if (orders.Count == 0)
            {
                Console.WriteLine("无订单");
                return;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("Order.xml", FileMode.Create))
            {

                xmlSerializer.Serialize(fs, orders);

            }
            Console.WriteLine("Serilaied Successfully");
        }
        public void Import()
        {
            using (FileStream fs = new FileStream("Order.xml", FileMode.Open))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
                orders = (List<Order>)xmlSerializer.Deserialize(fs);
                Console.WriteLine("Deserilaied Sucessfully");
                orders.ForEach(p => Console.WriteLine(p));
            }
        }
    }

    
    public class test
    {

        public static void Main(string[] args)
        {
            OrderService orderService = new OrderService();
            while (true)
            {
                Console.WriteLine("输入想那个要进行的操作：");
                Console.WriteLine("1添加订单 2删除订单 3修改订单 4订单号查询订单 5客户查询订单 6金额查询订单 7查询所有订单");
                Console.WriteLine("8ID排序 9按价格排序 10导出订单 11导入订单 12退出");
                int n = int.Parse(Console.ReadLine());
                switch (n)
                {
                    case 1:
                        orderService.AddOrder();
                        break;
                    case 2:
                        orderService.DelOrder();
                        break;
                    case 3:
                        orderService.ChangeOrder();
                        break;
                    case 4:
                        orderService.searchByID();
                        break;
                    case 5:
                        orderService.searchByCus();
                        break;
                    case 6:
                        orderService.searchByAmount();
                        break;
                    case 7:
                        orderService.searchAll();
                        break;
                    case 9:
                        orderService.orders.Sort((o1, o2) => o1.Amount - o2.Amount);
                        break;
                    case 8:
                        orderService.orders.Sort((o1, o2) => o1.Id - o2.Id);
                        break;
                    case 10:
                        orderService.Export();
                        break;
                    case 11:
                        orderService.Import();
                        break;
                    case 12:
                        return;

                    default: Console.WriteLine("错误"); break;
                }
            }
        }
    }

}
